from config import HOST
from tools import GetLog

logger = GetLog.get_logger()
class ApiRegisterLogin:
    def __init__(self, session):
        # 获取session对象
        self.session = session

        # 图片验证码URL
        self.__url_img_code = HOST + "/common/public/verifycode1/{}"
        # 短信验证码URL
        self.__url_phone_code = HOST + "/member/public/sendSms"
        # 注册接口URL
        self.__url_register_code = HOST + "/member/public/reg"
        # 登录接口URL
        self.__url_login_code = HOST + "/member/public/login"
        # 查询登录状态URL
        self.__url_login_status_code = HOST + "/member/public/islogin"
        logger.info("正在初始化session对象和URL")

    # 1、获取图片验证码
    def api_img_code(self, num):
        logger.info("正在获取图片验证码,请求方法:get,请求URL:{},请求数据:{}".format(self.__url_img_code.format(num),num))
        return self.session.get(url=self.__url_img_code.format(num))

    # 2、获取短信验证码
    def api_phone_code(self, phone, imgVerifyCode):
        phone_data = {
            "phone": phone,
            "imgVerifyCode": imgVerifyCode,
            "type": "reg"
        }
        logger.info("获取短信验证码,请求方法:get,请求URL:{},请求数据:{}".format(self.__url_phone_code, phone_data))
        return self.session.post(url=self.__url_phone_code, data=phone_data)

    # 3、注册接口
    def api_register(self, phone, password, verifycode, phone_code,dy_server,invite_phone):
        register_data = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code,
            "dy_server": dy_server,
            "invite_phone": invite_phone
        }
        logger.info("调用注册接口,请求方法:get,请求URL:{},请求数据:{}".format(self.__url_register_code, register_data))
        return self.session.post(url=self.__url_register_code, data=register_data)

    # 4、登录接口
    def api_login(self, keywords="17800001111", password="admin123"):
        login_data = {
            "keywords": keywords,
            "password": password
        }
        logger.info("调用登录接口,请求方法:get,请求URL:{},请求数据:{}".format(self.__url_login_code, login_data))
        return self.session.post(url=self.__url_login_code, data=login_data)

    # 5、验证登录接口
    def api_login_status(self):
        logger.info("调用验证登录接口,请求方法:get,请求URL:{}".format(self.__url_login_status_code))
        return self.session.post(url=self.__url_login_status_code)
